package com.mb.app.appused.views;

import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.StateListDrawable;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.widget.TextView;

public class ViewSelector {

	static int[] nomalState = {};

	static int[] focusedState = { android.R.attr.state_focused,
			android.R.attr.state_window_focused };

	static int[] nocheckState = { -android.R.attr.state_checked };

	static int[] checkState = { android.R.attr.state_window_focused,
			android.R.attr.state_checked };

	static int[] pressedState = { android.R.attr.state_window_focused,
			android.R.attr.state_pressed };

	public enum IconPoint {
		left, right, up, dowm, background, non
	};
	
	public static void setPressColor(View view, int nomal, int focuse, Boolean background) {
		if (view == null)
			return;
		int[] colors = new int[] { focuse, focuse, nomal };
		int[][] states = new int[3][];
		states[0] = pressedState;
		states[1] = focusedState;
		states[2] = nomalState;
		ColorStateList colorStateList = new ColorStateList(states, colors);
		// view.setBackgroundTintList(colorStateList);Drawable.setTintMode(PorterDuff.Mode)

		if (background) {
			//view.setBackgroundTintList(colorStateList);
			ViewCompat.setBackgroundTintList(view, colorStateList);
			ViewCompat.setBackgroundTintMode(view, PorterDuff.Mode.DARKEN);
		} else {
			if (view instanceof TextView)
				((TextView) view).setTextColor(colorStateList);
		}
	}

	public static void setFocused(View view, int nomal, int focuse, Rect rect) {
		setFocused(view, nomal, focuse, rect, IconPoint.non);
	}

	@SuppressWarnings("deprecation")
	public static void setFocused(View view, int nomal, int focuse, Rect rect,
			IconPoint point) {
		if (view == null)
			return;
		StateListDrawable drawable = new StateListDrawable();
		// Focused states
		drawable.addState(focusedState, view.getContext().getResources()
				.getDrawable(focuse));

		// Pressed
		drawable.addState(pressedState, view.getContext().getResources()
				.getDrawable(focuse));

		// Non focused states
		drawable.addState(nocheckState, view.getContext().getResources()
				.getDrawable(nomal));
		if (rect == null) {
			setIcon(view, drawable, point);
		} else {
			setIcon(view, drawable, rect, point);
		}
	}
	
	public static void setCheckColor(View view, int nomal, int check, Boolean background) {
		if (view == null)
			return;
		int[] colors = new int[] { nomal, check, nomal };
		int[][] states = new int[3][];
		states[0] = nocheckState;
		states[1] = checkState;
		states[2] = nomalState;
		ColorStateList colorStateList = new ColorStateList(states, colors);
		// view.setBackgroundTintList(colorStateList);
		if (background) {
			ViewCompat.setBackgroundTintList(view, colorStateList);
			ViewCompat.setBackgroundTintMode(view, PorterDuff.Mode.DARKEN);
		} else {
			if (view instanceof TextView)
				((TextView) view).setTextColor(colorStateList);
		}
	}
	
	public static void setCheck(View view, int nomal, int check, Rect rect) {
		setCheck(view, nomal, check, rect, IconPoint.non);
	}

	@SuppressWarnings("deprecation")
	public static void setCheck(View view, int nomal, int check, Rect rect,
			IconPoint point) {
		if (view == null)
			return;
		StateListDrawable drawable = new StateListDrawable();
		drawable.addState(checkState, view.getContext().getResources()
				.getDrawable(check));
		drawable.addState(nomalState, view.getContext().getResources()
				.getDrawable(nomal));
		// setIcon(view, drawable, point);
		if (rect == null) {
			setIcon(view, drawable, point);
		} else {
			setIcon(view, drawable, rect, point);
		}
	}

	@SuppressWarnings("deprecation")
	static void setIcon(View view, StateListDrawable drawable, IconPoint point) {

		view.setBackgroundDrawable(null);

		switch (point) {
		case dowm:
			if (view instanceof TextView)
				((TextView) view).setCompoundDrawablesWithIntrinsicBounds(null,
						null, null, drawable);
			break;
		case up:
			if (view instanceof TextView)
				((TextView) view).setCompoundDrawablesWithIntrinsicBounds(null,
						drawable, null, null);
			break;
		case left:
			if (view instanceof TextView)
				((TextView) view).setCompoundDrawablesWithIntrinsicBounds(
						drawable, null, null, null);

			break;
		case right:
			if (view instanceof TextView)
				((TextView) view).setCompoundDrawablesWithIntrinsicBounds(null,
						null, drawable, null);
			break;
		case background:
			view.setBackgroundDrawable(drawable);
			break;
		case non:
			view.setBackgroundDrawable(drawable);
			break;
		}
		// .setButtonDrawable(android.R.color.transparent);
	}

	@SuppressWarnings("deprecation")
	static void setIcon(View view, StateListDrawable drawable, Rect rect,
			IconPoint point) {

		view.setBackgroundDrawable(null);
		rect.right = DisplayUtils.dp2px(view.getContext(), rect.width());
		rect.bottom = DisplayUtils.dp2px(view.getContext(), rect.height());
		drawable.setBounds(rect);
		switch (point) {
		case dowm:
			if (view instanceof TextView)
				((TextView) view).setCompoundDrawables(null, null, null,
						drawable);
			break;
		case up:
			if (view instanceof TextView)
				((TextView) view).setCompoundDrawables(null, drawable, null,
						null);
			break;
		case left:
			if (view instanceof TextView)
				((TextView) view).setCompoundDrawables(drawable, null, null,
						null);

			break;
		case right:
			if (view instanceof TextView)
				((TextView) view).setCompoundDrawables(null, null, drawable,
						null);
			break;
		case background:
			view.setBackgroundDrawable(drawable);
			break;
		case non:
			view.setBackgroundDrawable(drawable);
			break;
		}
	}
}